package servicebctp

import (
	"context"
	"fmt"
	"github.com/aberic/gnomon"
	"github.com/aberic/gnomon/log"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/chain/bctp"
)

// Add 用户新增
func Add(url string, in *bctp.ReqUserAdd) (*bctp.Resp, error) {
	log.Info("User add url request:" + in.Url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Add(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			// return &bctp.Resp{
			// 	JsonRpc: "2.0",
			// 	Result: &bctp.Result{
			// 		Status: "OK",
			// 	},
			// 	Id: 0,
			// }, nil
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "Add user failed")
}

// Enroll 用户登录/注册
func Enroll(url string, in *bctp.ReqUserEnroll) (*bctp.RespUserEnroll, error) {
	log.Info("User enroll url request:" + in.Url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Enroll(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.RespUserEnroll); ok {
			log.Info("Enroll", log.Field("response", response))
			// return &bctp.RespUserEnroll{
			// 	OK:    "OK",
			// 	Error: "",
			// }, nil
			if gnomon.StringIsNotEmpty(response.Error) {
				return nil, errors.New(response.Error)
			}
			return response, err
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "Enroll user failed")
}

// UserTCertDel 删除证书
func UserTCertDel(url string, in *bctp.ReqUserTCertDel) (*bctp.Resp, error) {
	log.Info("Tcert delete url request:" + in.Url)
	// log.Info("UserTCertDel", log.Field("bctp.ReqUserTCertDel", in))
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.UserTCertDel(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "UserTCertDel failed")
}

// BindChannel 用户绑定通道
func BindChannel(url string, in *bctp.ReqUserChannelBind) (*bctp.Resp, error) {
	log.Info("User bind channel url request:" + in.Url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.BindChannel(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			// return &bctp.Resp{
			// 	JsonRpc: "2.0",
			// 	Result: &bctp.Result{
			// 		Status: "ok",
			// 	},
			// }, nil
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "BindChannel failed")
}

// BindChannelShip 用户和通道关系检索
func BindChannelShip(url string, in *bctp.ReqUserChannelBindShip) (*bctp.Resp, error) {
	log.Info("Get User channel bind ship url request:" + in.Url)
	log.Info("BindChannelShip", log.Field("ReqUserChannelBindShip", in))
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.BindChannelShip(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			// return &bctp.Resp{
			// 	JsonRpc: "2.0",
			// 	Result: &bctp.Result{
			// 		Status: "ok",
			// 		Message: `
			// 			{
			// 				"icbc.channel.permissions":{
			// 					"a_test":"common ccc"
			// 				}
			// 			}
			// 		`,
			// 	},
			// }, nil
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "BindChannel failed")
}

// BindCertShip 用户和证书关系检索
func BindCertShip(url string, in *bctp.ReqUserCertBindShip) (*bctp.Resp, error) {
	log.Info("Get User cert bind ship url request:" + in.Url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.BindCertShip(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			// return &bctp.Resp{
			// 	JsonRpc: "2.0",
			// 	Result: &bctp.Result{
			// 		Status:  "OK",
			// 		Message: "",
			// 	},
			// 	Id: 0,
			// }, nil
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "... failed")
}

// BindCert 用户和通道关系检索
func BindCert(url string, in *bctp.ReqUserCertBind) (*bctp.Resp, error) {
	log.Info("User bind cert url request:" + in.Url)
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewUserClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.BindCert(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.Resp); ok {
			fmt.Println(response)
			if response.Result.Error != nil {
				return nil, fmt.Errorf("code: %d, error: %s", response.Result.Error.Code, response.Result.Error.Data)
			}
			return response, nil
		}
		return nil, errors.New("can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "... failed")
}
